<?php
namespace app\admin\controller;
use think\admin\Controller;

use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Sms\V20210111\SmsClient;
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;

class Sms extends Controller
{
    public function index()
    {
        try {
            //配置$cred = new Credential("SecretId", "SecretKey");
            $cred = new Credential("AKIDxOl4DAyf9QWQ9e0Rltp4B65wlQq6DE3d", "UmriS1QhngLnLl3CB3oNm6JLCtTpwzuh");
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");
            
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
        
            $req = new SendSmsRequest();

            //手机号
            $phone = "15347584718";
            //随机生成6位验证码,strval转为字符串类型
            $code01 = strval(mt_rand(100000,999999));
            $params = array(
                "PhoneNumberSet" => array('+86'.$phone),
                "SmsSdkAppId" => "1400680496",
                "SignName" => "众创生活科技",
                "TemplateId" => "1407534",
                "TemplateParamSet" => array( $code01, "5" )
            );
            $req->fromJsonString(json_encode($params));
        
            $resp = $client->SendSms($req);
        
            print_r($resp->toJsonString());
        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }
}
